<?php
/**
 * [WeEngine System] Copyright (c) 2014 WE7.CC
 * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
 */

require __DIR__ . '/framework/bootstrap.inc.php';

$host = $_SERVER['HTTP_HOST'];
if (!empty($host)) {
	$bindhost = pdo_fetch("SELECT * FROM " . tablename('site_multi') . " WHERE bindhost = :bindhost", array(':bindhost' => $host));
	if (!empty($bindhost)) {
		header("Location: " . $_W['siteroot'] . 'app/index.php?i=' . $bindhost['uniacid'] . '&t=' . $bindhost['id']);
		exit;
	}
	
}
if ($_W['os'] == 'mobile' && (!empty($_GPC['i']) || !empty($_SERVER['QUERY_STRING']))) {
	header('Location: ' . $_W['siteroot'] . 'app/index.php?' . $_SERVER['QUERY_STRING']);
} else {
	if (!empty($_SERVER['QUERY_STRING'])) {
		header('Location: ' . $_W['siteroot'] . 'web/index.php?' . $_SERVER['QUERY_STRING']);
	} else {
		if (!empty($_SERVER['HTTP_SEC_FETCH_DEST']) && 'iframe' == $_SERVER['HTTP_SEC_FETCH_DEST']) {
			header('Location: ' . $_W['siteroot'] . 'web/index.php?c=home&a=home&do=home?#/platform');
			exit;
		}
		$settings = $_W['setting'];
		
		
		if (!empty($_W['setting']['copyright']['local_install'])) {
			header('Location:' . wurl('user/login'));
		} else {
			$cache_key = cache_system_key('zhida_content');
			$cache = cache_load($cache_key);
			if (!empty($cache)) {
				die($cache);
			}
			$iframe = 'https://zhida.w7.cc/indexIframe?site_key=' . $_W['setting']['site']['key'];
			$response = ihttp_get($iframe);
			if ($response['code'] == 200 && !empty($response['content'])) {
				$error = json_decode($response['content'], true);
				if (null === $error) {
					cache_write($cache_key, $response['content'], CACHE_EXPIRE_LONG);
				}
				die($response['content']);
			}
			die(ierror_page('服务异常（应用直达），请尽快联系管理员处理！'));
		}
	}
}
